package GUI;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;

import Controller.Client;
import Entity.Order;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Date;

/**
 * this is the GUI for information station order check in. the customer will only enter his car number.
 * @author Or Marx
 */
public class InformationStationOrderCheckInGui extends JPanel{
	JFrame frame;
	JPanel lastPanel;
	private JTextField textField;
	public void setLastPanel (JPanel panel)
	{
		this.lastPanel=panel;
	}
	public void setFrame (JFrame frame)
	{
		this.frame=frame;
	}
	/**
	 * is the constructor. it has only a button to confirm and validate the car number.
	 */
	public InformationStationOrderCheckInGui() {
		setLayout(null);
		
		JLabel lblPleaseEnterThe = new JLabel("please enter the car number registered in the order");
		lblPleaseEnterThe.setFont(new Font("Tahoma", Font.PLAIN, 18));
		lblPleaseEnterThe.setBounds(270, 34, 420, 40);
		add(lblPleaseEnterThe);
		
		textField = new JTextField();
		textField.setBounds(390, 160, 207, 20);
		add(textField);
		textField.setColumns(10);
		
		JLabel lblCarNumber = new JLabel("car number");
		lblCarNumber.setBounds(274, 163, 92, 14);
		add(lblCarNumber);
		
		JButton btnConfirm = new JButton("confirm");
		btnConfirm.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int flag=0;	//is there an order?
				Order [] orders=Client.getOrders();
				for (int i=0; i<orders.length; i++)
					if (orders[i].getCarNumber().equals(textField.getText()) && orders[i].getArrivalDate().compareTo(new Date())<0 && orders[i].getLeavingDate().compareTo(new Date())>0 && orders[i].getParkingLotNumber()==Client.getParkingLot())
					{
						orders[i].setActualArrivalDate(new Date());
						Client.UpdateOrderInDB(orders[i]);
						if(Client.getParkingLots_sorted()[Client.getParkingLot()-1].insertCar(orders[i].getCarNumber(), orders[i].getLeavingDate()))
							{
								JOptionPane.showMessageDialog(null,"enjoy your parking!","check in",JOptionPane.INFORMATION_MESSAGE);
								frame.setContentPane((JPanel)lastPanel);
							}
						else
							JOptionPane.showMessageDialog(null,"the parking lot is full, talk to johnson for tech support","check in",JOptionPane.INFORMATION_MESSAGE);
						flag=1;
						break;
					}
			if (flag==0)
				JOptionPane.showMessageDialog(null,"no order for this car number to this parking lot at this time (maby you are early)","check in",JOptionPane.ERROR_MESSAGE);
			
			}
		});
		btnConfirm.setBounds(316, 309, 89, 23);
		add(btnConfirm);
		
		JButton btnCancel = new JButton("cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				frame.setContentPane((JPanel)lastPanel);
			}
		});
		btnCancel.setBounds(579, 309, 89, 23);
		add(btnCancel);
	}
	
}
